package com.google.appengine.codelab;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

public class Datastore {
	
	public static PreparedQuery get(String kind, Filter filter) {
		Query query = new Query(kind);
		if (filter != null) {
			query.setFilter(filter);
		}
		return Util.getDatastoreServiceInstance().prepare(query);
	}
	
	public static Entity get(String kind, long id) {
		Key key = KeyFactory.createKey(kind, id);
		return Util.findEntity(key); 		
	}
	
	public static PreparedQuery get(String kind, String propertyName, Object value) {
		Query query = new Query(kind);
		if ((value != null) && (!"".equals(value))) {
			query.setFilter(new FilterPredicate(propertyName, FilterOperator.EQUAL, value));
		}
		return Util.getDatastoreServiceInstance().prepare(query);
	}
	
}
